<?php
namespace Common\Model;

/**
 * 房型上架信息表
 * @modify trainyao 2016-01-04 创建
 * @index roomtypedetail` (`isdelete`, `hotelentity_id`,`roomtype_id`)
 */

class RoomTypeDetailModel extends BaseModel
{
    protected $trueTableName = 'bk_room_type_detail';

    /**
     * 增加一条或多条信息
     * @modify trainyao 2016-04-29  创建
     * @param  integer   $hotelId    客栈id
     * @param  mixin   $roomTypeId   房型id,一个或多个
     * @param  mixin   $enable       初始化启用状态,默认是启用
     * @return boolean                 执行结果
     */
    public function addInfo($hotelId, $roomTypeId, $enable = 1) {
        if (!is_array($roomTypeId)) {
            $roomTypeId = explode(',', $roomTypeId);
        }

        $data = array();
        if (count($roomTypeId) > 1) {
            $creator = getUserId();
            foreach ($roomTypeId as $roomTypeIdItem) {
                array_push($data, array(
                    'hotelentity_id' => $hotelId,
                    'roomtype_id' => $roomTypeIdItem,
                    'creator' => $creator,
                    'operator' => $creator,
                    'enable' => $enable,
                ));
            }
            unset($roomTypeIdItem);

            return $this->addAll($data) ? true : false;
        } else {
            $data['hotelentity_id'] = $hotelId;
            $data['roomtype_id'] = $roomTypeId[0];
            $data['enable'] = $enable;

            return $this->add($data) ? true : false;
        }
    }

    /**
     * 根据客栈查询信息
     * @modify trainyao 2016-04-29 创建
     * @modify wenhui 2017-01-09 添加$isEnable
     * @param  integer   $hotelId   客栈ID
     * @param  bool   $isEnable   是否获取没启用的房型
     * @return array                信息数组
     */
    public function getInfoByHotel($hotelId, $isEnable = false) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );
        if ($isEnable) {
            $option['enable'] = 1;
        }

        $data = $this->where($option)->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据id查询信息
     * @modify trainyao 2016-04-29 创建
     * @param  integer   $hotelId    客栈id
     * @param  mixin   $roomTypeId    房型id
     * @return array                信息数组
     */
    public function getInfoByRoomType($hotelId = null, $roomTypeId,$isEnable = false) {
        $option = array(
            'isdelete' => NOT_DELETE,
        );
        if ($isEnable) {
            $option['enable'] = 1;
        }

        if (!is_null($hotelId)) {
            $option['hotelentity_id'] = $hotelId;
        }
        if (is_array($roomTypeId)) {
            $roomTypeId = implode(',', $roomTypeId);
        }
        if (strpos($roomTypeId, ',') === false) {
            $option['roomtype_id'] = $roomTypeId;
            $data = $this->where($option)->find();
        } else {
            $option['roomtype_id'] = array('in', $roomTypeId);
            $data = $this->where($option)->select();
        }
        return empty($data) ? array() : $data;
    }

    /**
     * 根据客栈房型查询信息
     * @modify trainyao 2016-04-29 创建
     * @param  integer   $hotelId   客栈ID
     * @param  mixin   $roomTypeId   房型ID
     * @return array                信息数组
     */
    public function getInfoByHotelRoomType($hotelId, $roomTypeId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );
        if (is_array($roomTypeId)) {
            $roomTypeId = implode(',', $roomTypeId);
        }
        if (strpos($roomTypeId, ',') === false) {
            $option['roomtype_id'] = $roomTypeId;
            $data = $this->where($option)->find();
        } else {
            $option['roomtype_id'] = array('in' ,$roomTypeId);
            $data = $this->where($option)->select();
        }
        return empty($data) ? array() : $data;
    }

    /**
     * 根据一个或多个房型ID删除上架房型数据
     * @modify trainyao 2016-01-04 创建
     * @param  integer   $hotelId    客栈id
     * @param  mixin   $roomTypeId    房型id(s)
     * @return boolean 执行结果
     */
    public function deleteInfoByRoomType($hotelId, $roomTypeId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );

        if(is_array($roomTypeId)) {
            $roomTypeId = implode(',', $roomTypeId);
        }
        if(strpos($roomTypeId, ',') !== false) {
            $option['roomtype_id'] = array('in', $roomTypeId);
        } else {
            $option['roomtype_id'] = $roomTypeId;
        }

        $data = array(
            'isdelete' => IS_DELETE,
        );

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 根据一个或多个房型ID,设置禁用或启用
     * @modify trainyao 2016-05-04 创建
     * @param  integer   $hotelId    客栈id
     * @param  mixin   $roomTypeId    房型id(s)
     * @param  mixin   $enable    启用或禁用
     * @return boolean 执行结果
     */
    public function saveEnableByRoomType($hotelId, $roomTypeId, $enable) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );

        if(is_array($roomTypeId)) {
            $roomTypeId = implode(',', $roomTypeId);
        }
        if(strpos($roomTypeId, ',') !== false) {
            $option['roomtype_id'] = array('in', $roomTypeId);
        } else {
            $option['roomtype_id'] = $roomTypeId;
        }

        $data = array(
            'enable' => $enable,
        );

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 保存房型上架基本信息
     * @modify trainyao 2016-01-04 创建
     * @return boolean 执行结果
     */
    /**
     * 保存房型上架基本信息
     * @modify trainyao 2016-05-03   创建
     * @param  integer   $hotelId    客栈id
     * @param  integer   $roomTypeId    房型id
     * @param  string   $price       价格
     * @param  string   $description 房型上架描述
     * @param  string   $coverImageUrl  封面图片url
     * @return boolena                执行结果
     */
    public function saveInfo($hotelId, $roomTypeId, $price = null, $description = null, $coverImageUrl = null) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
            'roomtype_id' => $roomTypeId,
        );

        $data = array();
        if (!is_null($price)) {
            $data['price'] = $price;
        }
        if (!is_null($description)) {
            $data['description'] = $description;
        }
        if (!is_null($coverImageUrl)) {
            $data['coverimageurl'] = $coverImageUrl;
        }


        return $this->where($option)->save($data) === false ? false : true;
    }
}
